/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * RSCamerasLibrary.cpp
 *
 *  Created on: 2010/03/07
 *      Author: Alexandre Pires ( c.alexandre.pires@gmail.com )
 */

#include "RSCamerasLibrary.h"
#include "RSCamera.h"

namespace Realsphere {

    RSCamerasLibrary::RSCamerasLibrary ( const QString& rId, const QString& rName ) :
        RSUIDElement ( rId, RS_CAMERAS_LIBRARY_TAG )
    {
        SetName ( rName, false );
    }

    RSCamerasLibrary::~RSCamerasLibrary ()
    {
        mCameras.clear ();
    }

    RSCamera* RSCamerasLibrary::GetCamera ( unsigned int rIndex ) const
    {
        return mCameras[rIndex];
    }

    RSCamera* RSCamerasLibrary::GetCamera ( const QString& rId ) const
    {
        RSCamerasIterator iterator = RSCamerasIterator ( mCameras );

        while ( iterator.hasNext () )
        {
            RSCamera* current_geometry = iterator.next ();
            QString geometry_id = current_geometry->Id ();

            if ( geometry_id == rId )
                return current_geometry;
        }

        return NULL;
    }

    int RSCamerasLibrary::IndexOf ( RSCamera* rCamera )
    {
        return mCameras.indexOf ( rCamera );
    }

    unsigned int RSCamerasLibrary::CamerasSize ()
    {
        return mCameras.size ();
    }

    RSCameras RSCamerasLibrary::Cameras () const
    {
        return mCameras;
    }

    RSCamerasIterator RSCamerasLibrary::CamerasIterator () const
    {
        return RSCamerasIterator ( mCameras );
    }

    bool RSCamerasLibrary::IsRootElement () const
    {
        return false;
    }

    RSError RSCamerasLibrary::BeforeAddChildElement ( RSElement* rChild )
    {
        RSError error = RSElement::BeforeAddChildElement ( rChild );

        if ( error == RS_INVALID_ELEMENT )
        {

            if ( rChild->InstanceOf ( RS_CAMERA_CLASS ) )
                error = RS_NO_ERROR;

        }

        return error;
    }

    bool RSCamerasLibrary::AfterAddChildElement ( RSElement* rChild )
    {
        bool handled = RSElement::AfterAddChildElement ( rChild );

        if ( !handled && rChild->InstanceOf ( RS_CAMERA_CLASS ) )
        {

            RSCamera* geometry = RSCameraCast ( rChild );
            mCameras.append ( geometry );
            handled = true;

            emit CameraAdded ( this, geometry );
        }

        return handled;
    }

    bool RSCamerasLibrary::AfterRemoveChildElement ( RSElement* rChild )
    {
        bool handled = RSElement::AfterRemoveChildElement ( rChild );

        if ( !handled && rChild->InstanceOf ( RS_CAMERA_CLASS ) )
        {
            RSCamera* geometry = RSCameraCast ( rChild );
            int index = mCameras.indexOf ( geometry );
            mCameras.removeAt ( index );
            handled = true;

            emit CameraRemoved ( this, geometry );
        }

        return handled;
    }

    RSError RSCamerasLibrary::AddCamera ( RSCamera* rCamera )
    {
        RSError error = AddChildElement ( rCamera );
        return error;
    }

    RSError RSCamerasLibrary::RemoveCamera ( RSCamera* rCamera )
    {
        RSError error = RemoveChildElement ( rCamera );
        return error;
    }

    RSError RSCamerasLibrary::RemoveCamera ( const QString& rId )
    {
        RSCamera* geometry = GetCamera ( rId );
        RSError error = RemoveCamera ( geometry );
        return error;
    }

    RSError RSCamerasLibrary::RemoveCamera ( unsigned int rIndex )
    {
        RSCamera* geometry = GetCamera ( rIndex );
        RSError error = RemoveCamera ( geometry );
        return error;
    }
}
